package com.sevenyear.excelanalyze.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenyear.excelanalyze.common.result.Result;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.entity.param.queryConditionParam.PartitionBaseInfoQueryCondition;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 分区基本信息 前端控制器
 * </p>
 *
 * @author sevenyear
 * @since 2021-08-20
 */
@Api(value = "分区基本信息", tags = "02-分区基本信息相关接口")
@RestController
@CrossOrigin
@RequestMapping("/partitionBaseInfo")
public class PartitionBaseInfoController {

    @Autowired
    PartitionBaseInfoService partitionBaseInfoService;


    @ApiOperation(value = "Excel批量导入分区基本信息")
    @PostMapping("/insertPartition/excel")
    public Result insertPartitionByExcel(MultipartFile file) {
        partitionBaseInfoService.insertPartitionByExcel(file, partitionBaseInfoService);
        return Result.ok();
    }


    @ApiOperation(value = "手动添加分区基本信息")
    @PostMapping("/insertPartition/manual")
    public Result insertPartitionByManual(@RequestBody PartitionBaseInfo partitionBaseInfo) {
        boolean flag = partitionBaseInfoService.save(partitionBaseInfo);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "根据id删除单条分区基本信息记录")
    @GetMapping("/deletePartition/{id}")
    public Result deletePartitionById(@PathVariable long id) {
        boolean flag = partitionBaseInfoService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "根据id修改分区基本信息")
    @PostMapping("/updatePartiton")
    public Result updatePartitionById(@RequestBody PartitionBaseInfo partitionBaseInfo) {
        boolean flag = partitionBaseInfoService.updateById(partitionBaseInfo);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    @ApiOperation(value = "查询所有分区基本信息，供excel导出使用")
    @GetMapping("/query/allPartition")
    public Result queryAllPartitionBaseInfo() {
        List<PartitionBaseInfo> allPartitionBaseInfoList = partitionBaseInfoService.list(null);
        return Result.ok().data("allPartitionBaseInfoList", allPartitionBaseInfoList);
    }

    @ApiOperation(value = "条件带分页查询分区基本信息")
    @PostMapping("/query/{current}/{limit}")
    public Result queryPagePartitionCondition(@PathVariable long current, @PathVariable long limit,
                                              @RequestBody(required = false) PartitionBaseInfoQueryCondition partitionBaseInfoQueryCondition) {
        //创建page对象
        Page<PartitionBaseInfo> pagePartition = new Page<>(current, limit);
        partitionBaseInfoService.queryPageByCondition(pagePartition, partitionBaseInfoQueryCondition);
        long total = pagePartition.getTotal();
        List<PartitionBaseInfo> records = pagePartition.getRecords();
        return Result.ok().data("total", total).data("list", records);

    }


    @ApiOperation(value = "查询所有地貌信息")
    @GetMapping("/query/allLandform")
    public Result queryAllLanform() {
        List<PartitionBaseInfo> landformList = partitionBaseInfoService.queryAllLandform();
        return Result.ok().data("landformList", landformList);
    }

    @ApiOperation(value = "查询所有粘土厚度信息")
    @GetMapping("/query/allClayThickness")
    public Result queryAllClayThickness() {
        List<PartitionBaseInfo> clayThicknessList = partitionBaseInfoService.queryAllClayThickness();
        return Result.ok().data("clayThicknessList", clayThicknessList);
    }

    @ApiOperation(value = "根据地貌和粘土厚度查询地区编码")
    @PostMapping("/query/partitionCode")
    public Result queryPartitionCode(@RequestBody(required = false) PartitionBaseInfoQueryCondition partitionBaseInfoQueryCondition) {
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform",partitionBaseInfoQueryCondition.getLandform());
        wrapper.eq("clay_thickness",partitionBaseInfoQueryCondition.getClayThickness());
        System.out.println(partitionBaseInfoQueryCondition.getClayThickness());
        System.out.println(partitionBaseInfoQueryCondition.getLandform());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        System.out.println(partitionBaseInfo);
        return Result.ok().data("partitionCode",partitionBaseInfo.getPartitionCode()).data("partitionId",partitionBaseInfo.getId());
    }

}

